package com.algorithm;

/**
 * 按奇偶排序数组
 */
public class SortArrayByParity {


    public static int[] sortArrayByParity(int[] nums) {
        int i = 0;
        int j = nums.length - 1;
        while (i <= j) {
            if (nums[i] % 2 == 1 && nums[j] % 2 == 0) {
                nums[i] = (nums[j] + nums[i]) - (nums[j] = nums[i]);
                i++;
                j--;
            } else if (nums[i] % 2 == 1 && nums[j] % 2 == 1) {
                j--;
            }else if(nums[i] % 2 == 0 && nums[j] % 2 == 0){
                i++;
            }else {
                i++;
                j--;
            }
        }
        return nums;
    }

    public static void main(String[] args) {
//        int[] nums = {3, 1, 2, 4};
//        int[] nums = {1, 0, 3};
        int[] nums = {0,1};
        int[] array = sortArrayByParity(nums);
        System.out.println(array);
    }
}
